package cn.idietrade.idietradelogisticsmodule8011.controller;

import cn.idietrade.common.kdniao.KdniaoTrackQueryAPI;
import cn.idietrade.common.pojo.SecondLogistics;
import cn.idietrade.common.pojo.SecondUser;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.idietradelogisticsmodule8011.service.SecondLogisticsService;
import cn.idietrade.idietradelogisticsmodule8011.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.logging.Logger;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-12-04 17:55
 * @description
 */
@Slf4j
@RestController
public class LogisticsController {
    @Resource
    private SecondLogisticsService secondLogisticsService;

    @Resource
    private RedisUtil redisUtil;

    Logger logger = Logger.getLogger("LogisticsController");

    //跟踪物流信息查询
    @RequestMapping("/queryLogisticsInfo")
    public ReturnResultData queryLogisticsInfo(@RequestParam("expCode")String expCode,
                                               @RequestParam("expNo")String expNo){
        ReturnResultData resultData =new ReturnResultData<>();
        try {
            KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
            //参数1：expCode 快递公司代号（注：需要参考快递鸟提供的“快递鸟接口支持快递公司编码表.xlsx”）参数2：expNo 快递单号
            String result = api.getOrderTracesByJson(expCode, expNo);
            Object parse = JSON.parse(result);
            resultData.setCode(1);
            resultData.setMessage("物流信息跟踪查询成功");
            resultData.setData(parse);
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("物流信息跟踪查询程序异常");
            resultData.setData(null);
            logger.info("物流信息跟踪查询程序异常"+e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }

    @RequestMapping("/saveLogisticsInfo")
    public ReturnResultData saveLogisticsInfo(SecondLogistics secondLogistics){
        ReturnResultData resultData = new ReturnResultData<>();
        try {
            int i = secondLogisticsService.saveLogisticsInfo(secondLogistics);
            if(i>0){
                resultData.setCode(1);
                resultData.setMessage("保存物流信息成功");
                resultData.setData(null);
            }else{
                resultData.setCode(0);
                resultData.setMessage("保存物流信息失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("保存物流信息程序异常");
            resultData.setData(null);
            logger.info("保存物流信息程序异常"+e.getMessage());
            e.printStackTrace();
        }
        return  resultData;
    }

    @RequestMapping("/queryLogisticss")
    public ReturnResultData<PageInfo<SecondLogistics>> queryLogisticss(Integer pageNum, Integer pageSize,Integer orderId){
        ReturnResultData<PageInfo<SecondLogistics>> resultData = new ReturnResultData<>();
        try {
            PageInfo<SecondLogistics> secondLogisticsPageInfo = secondLogisticsService.queryLogisticss(pageNum, pageSize, orderId);
            if(EmptyUtils.isNotEmpty(secondLogisticsPageInfo)){
                resultData.setCode(1);
                resultData.setMessage("查询物流信息成功");
                resultData.setData(secondLogisticsPageInfo);
            }else{
                resultData.setCode(0);
                resultData.setMessage("查询物流信息失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("查询物流信息程序异常");
            resultData.setData(null);
            logger.info("查询物流信息程序异常"+e.getMessage());
            e.printStackTrace();
        }
        return  resultData;
    }


}
